
/* ______________________________________________________________________

	DropMenu Class
	This class handles dropdown menus.

	License: MIT License

	Vaca Framework
	http://vaca-framework.googlecode.com/
   ______________________________________________________________________
*/

Vaca.UI.DropMenu = new Class({
	ClassName: 'DropMenu',
	Extends: Vaca.BaseObject,
	Implements: Options,

	button: null,
	menu: null,
	isOpen: false,
	inProgress: false,

	options: {},

	initialize: function(menuButton, options) {
		this.parent();
		this.setOptions(options);
		this.button = menuButton;
		this.menu = menuButton.getNext('.vaca-dropmenu-menu');
		this.menu.setStyles({ visibility: 'hidden', opacity: 0 });

		// Hook events
		this.button.addEvent('click', function(event) {
			event.preventDefault();
			if (this.isOpen) this.hide(); else this.show();
		}.bind(this));
	},

	show: function() {
		if (this.inProgress) return;
		
		// this.sendMessageToPool({ targetClass: 'DropMenu', data: 'hide' });
		
		this.menu.setStyles({ display: 'block' });

		if (this.button.getPosition().y > $(document.body).getHeight() - this.menu.getHeight()) {
			posY = this.button.getPosition().y - this.menu.getHeight();
		} else {
			posY = this.button.getPosition().y + this.button.getHeight();
		}

		this.menu.setStyles({ top: posY, left: this.button.getPosition().x });

		this.inProgress = true;
		new Fx.Morph(this.menu, {
			onComplete: function() {
				this.isOpen = true;
				this.inProgress = false;
			}.bind(this)
		}).start({ opacity: 1 });
	},

	hide: function() {
		if (this.inProgress) return;

		this.inProgress = true;
		new Fx.Morph(this.menu, {
			onComplete: function() {
				this.menu.setStyles({
					display: 'block'
				});
				this.isOpen = false;
				this.inProgress = false;
			}.bind(this)
		}).start({ opacity: 0 });
	},

	processPoolMessage: function(sender, msg) {
		if (msg.targetClass != this.ClassName) return;
		console.log(msg);
		if (msg.data == 'show') this.show();
		if (msg.data == 'hide') this.hide();
	}
});
